//
//  UserSettingViewController.m
//  xpkc
//
//  Created by TopSage on 15/10/13.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "UserSettingViewController.h"
#import "IntroductionView.h"
#import "YLAppDelegate.h"
#import "UserMessage.h"
@interface UserSettingViewController (){
    UISwitch *remindSwitch;
    UISwitch *wifiSwitch;
    UISwitch *rememeberSwitch;
    UITableView *_tableView;
    UIButton *_login;
    UIView *_foot_View;
    CLLocationManager *locationManager;
}

@end

@implementation UserSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WhiteSmokeColor;
    [self creatView];
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 0;
    [UIView animateWithDuration:0.4 animations:^{
        self.navigationController.navigationBar.alpha = 1;
    }];
}

- (void)creatView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    CGRect head_frame;
    _tableView.backgroundColor = WhiteSmokeColor;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.bounces = NO;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    if (isPad) {
        head_frame = RECT(0, 0, 100, 100);
    }else{
        head_frame = RECT(0, 0, 100, 90);
    }
    
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [self.view addSubview:_tableView];
}

#pragma mark - =======tableView delegate & dataSource========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 4;
    } else if (section == 1) {
        return 2;
    }
    return 2;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (isPad) {
        return 80;
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0||indexPath.row == 3) {
            return 80;
        }
    }
    return 55;
}
//section之间的间隔
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
/**
 *   创建我的设置视图
 *
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.detailTextLabel.font = FONT_SYS_LITTLE;
    }
    
    switch (section) {
        case 0:
            if(row == 0)
            {
                cell.accessoryType=UITableViewCellAccessoryNone;
                cell.textLabel.text =  @"学习提醒";
                cell.detailTextLabel.text = @"根据学习进度定期进行学习提醒";
                CGRect switch_frame;
                if (IsIOS7) {
                    switch_frame = RECT(SCREEN_WIDTH-70, 7, 40, cell.frame.size.height);
                }else{
                    switch_frame = RECT(SCREEN_WIDTH-90, 7, 40, cell.frame.size.height);
                }
                remindSwitch = [[UISwitch alloc] initWithFrame:switch_frame];
                NSString *yesorno = [[NSUserDefaults standardUserDefaults] valueForKey:REMIND_STUDY];
                if([yesorno isEqualToString:@"NO"]) {
                    remindSwitch.on = NO;
                } else {
                    remindSwitch.on = YES; // 进行定期学习提醒
                }
                [self setREMIND_ONLYinfo];
                [remindSwitch addTarget:self action:@selector(changeREMIND_Switch) forControlEvents:UIControlEventValueChanged];
                [cell addSubview:remindSwitch];
                
            }else if(row == 1){
                cell.accessoryType=UITableViewCellAccessoryNone;
                cell.textLabel.text =  @"仅在WiFi下观看和下载";
                CGRect switch_frame;
                if (IsIOS7) {
                    switch_frame = RECT(SCREEN_WIDTH-70, 7, 40, cell.frame.size.height);
                }else{
                    switch_frame = RECT(SCREEN_WIDTH-90, 7, 40, cell.frame.size.height);
                }
                wifiSwitch = [[UISwitch alloc]initWithFrame:switch_frame];
                NSString * yesorno = [[NSUserDefaults standardUserDefaults]valueForKey:WIFI_ONLY];
                if ([yesorno isEqualToString:@"NO"]) {
                    wifiSwitch.on = NO;
                }else{
                    wifiSwitch.on = YES;//默认只在wifi下才能下载
                }
                //初始的时候就保存一下
                [self setWIFI_ONLYinfo];
                [wifiSwitch addTarget:self action:@selector(changeWIFI_ONLYswitch) forControlEvents:UIControlEventValueChanged];
                //                    wifiSwitch.tintColor = [UIColor orangeColor];
                [cell addSubview:wifiSwitch];
            }else if(row == 2){
                cell.accessoryType=UITableViewCellAccessoryNone;
                cell.textLabel.text = @"是否跳转上次播放位置";
                CGRect remember_frame;
                if (IsIOS7) {
                    remember_frame = RECT(SCREEN_WIDTH-70, 7, 40, cell.frame.size.height);
                }else{
                    remember_frame = RECT(SCREEN_WIDTH-90, 7, 40, cell.frame.size.height);
                }
                rememeberSwitch = [[UISwitch alloc]initWithFrame:remember_frame];
                NSString * yesorno = [[NSUserDefaults standardUserDefaults]valueForKey:REMEMBER_LAST];
                if ([yesorno isEqualToString:@"NO"]) {
                    rememeberSwitch.on = NO;
                }else{
                    rememeberSwitch.on = YES;//默认继续播放
                }
                [self setREMEMBER_ONLYinfo];
                [rememeberSwitch addTarget:self action:@selector(changeREMEMBER_LASTswitch) forControlEvents:UIControlEventValueChanged];
                [cell addSubview:rememeberSwitch];
            }else{
                
                cell.accessoryType=UITableViewCellAccessoryNone;
                cell.textLabel.text =  [NSString stringWithFormat:@"清除文件缓存（%0.1fM）",[GetFileSize folderSizeAtPath:DOC_PATH]];
                cell.detailTextLabel.text = @"清理本地下载的全部视频缓存";
                cell.selectionStyle = UITableViewCellSelectionStyleGray;
                
            }
            break;
        case 1:
            if(row == 0){
                cell.textLabel.text =  @"去评分";
            }else{
                cell.textLabel.text =  @"意见反馈";
            }
            break;
        case 2:
            if (row == 0) {
                cell.textLabel.text = @"功能介绍";
            } else {
                cell.textLabel.text = @"关于";
            }
            break;
        default:
            break;
    }
    return cell;
}
/**
 *   点击不同行，跳转不同
 *
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =  [_tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    // 改变是否定期提醒学习
                    [self changeREMIND_ONLYiswitch];
                    
                }
                    break;
                case 1:
                {
                    //改变wifi信息
                    [self changeWIFI_ONLY];
                }
                    break;
                case 2:
                {
                    //改变是否记录上次播放
                    [self changeREMEMBER_LAST];
                }
                    break;
                case 3:
                {
                    //清除缓存通知
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"确认要删除本地缓存文件吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                    alert.tag = 100;
                    [alert show];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    NSString *str2 = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@",  @"897012923"];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str2]];
                }
                    break;
                case 1:
                {
                    if (![UserMessage sharedUserMessage].isLogin) {
                        [self showAlertWithMessage:@"请先登陆"];
                    }else{
                   FeedbackViewController *vc=[[FeedbackViewController alloc]init];
                  [self.navigationController pushViewController:vc animated:YES];
                 if (IsIOS7) {
                if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
                  self.navigationController.interactivePopGestureRecognizer.delegate = nil;
                 }
                 }

                    
                }

            }
                    break;
                default:
                    break;
            }
            
        }
            break;
        case 2:
        {
            switch (indexPath.row) {
                case 0:
                {
                    logdebug(@"功能介绍!!!");
                    logdebug(@"再跑一遍引导页");
                    IntroductionView *IV = [[IntroductionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
                    YLAppDelegate *dele = [UIApplication sharedApplication].delegate;
                    IV.isNotIntroduction = NO;
                    [dele.window addSubview:IV];
                }
                    break;
                case 1:
                {
                    AboutViewController *vc=[[AboutViewController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                    if (IsIOS7) {
                        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
                            self.navigationController.interactivePopGestureRecognizer.delegate = nil;
                        }
                    }
                }
                    break;
                    
                default:
                    break;
            }
        }
            
        default:
            break;
    }
}
- (void)showAlertWithMessage:(NSString *)message {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
}


//前往appstore更新
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 100) {
        //确定
        //删除本地数据
        if (buttonIndex == 1) {
            //发送通知
            [[NSNotificationCenter defaultCenter]postNotificationName:CLEAR_DATA_REFRESH_TABLEVIEW object:nil];
            UITableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForItem:3 inSection:0]];
            cell.textLabel.text =  [NSString stringWithFormat:@"清除离线视频（0.0M）"];
        }
    }
}
//改变只在WiFi下下载的选项(cell的方法)
-(void)changeWIFI_ONLY{
    //如果只在wifi下的话就改变，并且将信息添加到本地的userdefault中
    if (wifiSwitch.on) {
        //
        [wifiSwitch setOn:NO animated:YES];
        logdebug(@"随便下");
        [self setWIFI_ONLYinfo];
    }else{
        [wifiSwitch setOn:YES animated:YES];
        //变成必须wifi
        logdebug(@"现在必须wifi下才行");
        [self setWIFI_ONLYinfo];
    }
}
//改变只在WiFi下下载的选项(switch的方法)
-(void)changeWIFI_ONLYswitch{
    if (!wifiSwitch.on) {
        //
        [wifiSwitch setOn:NO animated:YES];
        logdebug(@"随便下");
        [self setWIFI_ONLYinfo];
    }else{
        [wifiSwitch setOn:YES animated:YES];
        //变成必须wifi
        logdebug(@"现在必须wifi下才行");
        [self setWIFI_ONLYinfo];
    }
}
//改变只在WiFi下下载的选项(cell的方法)
-(void)changeREMEMBER_LAST{
    //如果只在wifi下的话就改变，并且将信息添加到本地的userdefault中
    if (rememeberSwitch.on) {
        //
        [rememeberSwitch setOn:NO animated:YES];
        logdebug(@"随便下");
        [self setREMEMBER_ONLYinfo];
    }else{
        [rememeberSwitch setOn:YES animated:YES];
        //变成必须wifi
        logdebug(@"现在必须wifi下才行");
        [self setREMEMBER_ONLYinfo];
    }
}
//改变是否跳转到播放位置
-(void)changeREMEMBER_LASTswitch{
    if (!rememeberSwitch.on) {
        //从头开始播放
        [rememeberSwitch setOn:NO animated:YES];
        logdebug(@"从头开始播放");
        [self setREMEMBER_ONLYinfo];
    }else{
        [rememeberSwitch setOn:YES animated:YES];
        //继续上次播放
        logdebug(@"继续上次播放");
        [self setREMEMBER_ONLYinfo];
    }
    
}
// 改变是否定期提醒学习
- (void)changeREMIND_Switch {
    if(!remindSwitch.on) {
        // 不进行学习提醒
        [remindSwitch setOn:NO animated:YES];
        [self setREMIND_ONLYinfo];
    } else {
        [remindSwitch setOn:YES animated:YES];
        [self setREMIND_ONLYinfo];
    }
}
// 改变是否定期提醒学习 (cell 方法)
- (void)changeREMIND_ONLYiswitch {
    if(remindSwitch.on) {
        // 不进行学习提醒
        [remindSwitch setOn:NO animated:YES];
        [self setREMIND_ONLYinfo];
    } else {
        [remindSwitch setOn:YES animated:YES];
        [self setREMIND_ONLYinfo];
    }
}
//将只在wifi下进行保存?本地化好呢还是暂存好呢
//本地化
-(void)setWIFI_ONLYinfo{
    [[NSUserDefaults standardUserDefaults]setValue:wifiSwitch.on == YES?@"YES":@"NO" forKey:WIFI_ONLY];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
//是否记住上次播放位置
-(void)setREMEMBER_ONLYinfo{
    [[NSUserDefaults standardUserDefaults]setValue:rememeberSwitch.on == YES?@"YES":@"NO" forKey:REMEMBER_LAST];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
// 是否进行定期学习提醒
- (void)setREMIND_ONLYinfo {
    [[NSUserDefaults standardUserDefaults] setValue:remindSwitch.on == YES ? @"YES":@"NO" forKey:REMIND_STUDY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
